using IQIGame.Onigao.Framework;

namespace IQIGame.Onigao.Game
{
    public class ReceivePackage : BasePacket, IConcurrentReusableClass
    {
#if UNITY_EDITOR
        ~ReceivePackage()
        {
            if (UnityEditor.EditorApplication.isPlaying)
            {
                LogGame.LogError("ReceivePackage is Be GC!!!");
            }
        }
#endif
        private PackageHeader m_header = new();
        [ClassPoolCheckIgnore]
        private SInputStream m_inputStream = new();

        //private static int counter = 0;
        public ReceivePackage()
        {
            //Debug.LogWarning($"New RecevicePackage counter = {++counter}");
        }

        public PackageHeader Header
        {
            get
            {
                return m_header;
            }
        }

        public SInputStream InputStream
        {
            get
            {
                return m_inputStream;
            }
        }

        public void Init(byte[] bytes, int offset, int length)
        {
            m_header.Init(bytes, offset);
            m_inputStream.Init(bytes, m_header.HeaderSize + offset, length - m_header.HeaderSize);
        }

        public void InitWithoutHeadLength(byte[] bytes, int offset, int length)
        {
            m_header.InitWithoutHeadLength(bytes, offset);
            m_inputStream.Init(bytes, m_header.HeaderSize + offset - HEAD_LENGTH, length - m_header.HeaderSize + HEAD_LENGTH);
        }

        public void Dispose()
        {
            m_active = false;
            m_header = null;
            if (m_inputStream != null)
            {
                m_inputStream.Dispose();
                m_inputStream = null;
            }
        }

        public void OnReset()
        {
            m_active = false;
            m_header.Clear();
            m_inputStream.Clear();
        }

    }
}


